#TODO: consistency issues in the fact that this modifies the velocity
#        directly, and not as a force?
from force_generator import ForceGenerator
from vector import Vector

class BoxFG(ForceGenerator):
    '''Simulate a box around the particles'''

    def __init__(self, _all_particles, generator_name, ul_bound, lr_bound):
        # bounds of the box
        self.ul_bound = ul_bound # upper-left bound
        self.lr_bound = lr_bound # lower-right bound
        ForceGenerator.__init__(self, _all_particles, generator_name)

    # % of velocity retained in collision
    RETAINED = .75

    def act_on_particle(self, particle):
        if not particle.has_attribute(self.generator_name + "_NoBoxCollision"):
            
            if particle.position.y > self.ul_bound.y: # particle is too high
                particle.position.y = self.ul_bound.y
                particle.velocity.y *= -self.RETAINED

            if particle.position.y < self.lr_bound.y: # particle is too low
                particle.position.y = self.lr_bound.y
                particle.velocity.y *= -self.RETAINED

            if particle.position.x < self.ul_bound.x: # particle is too left
                particle.position.x = self.ul_bound.x
                particle.velocity.x *= -self.RETAINED

            if particle.position.x > self.lr_bound.x: # particle is too right
                particle.position.x = self.lr_bound.x
                particle.velocity.x *= -self.RETAINED
